﻿#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class Solution
{
public:
    // 1046. 最后一块石头的重量
    int lastStoneWeight(vector<int>& stones)
    {
        priority_queue<int> q;
        for (auto x : stones)
            q.push(x);

        while (q.size() > 1)
        {
            int x = q.top();
            q.pop();
            int y = q.top();
            q.pop();
            int t = x - y;
            if (t)
                q.push(t);
        }
        return q.size() ? q.top() : 0;
    }
};

// 703. 数据流中的第 K 大元素
class KthLargest
{
    priority_queue<int, vector<int>, greater<int>> _heap;
    int _k;

public:
    KthLargest(int k, vector<int>& nums)
    {
        _k = k;
        for (auto x : nums)
        {
            _heap.push(x);
            if (_heap.size() > _k)
                _heap.pop();
        }
    }

    int add(int val)
    {
        _heap.push(val);
        if (_heap.size() > _k)
            _heap.pop();
        return _heap.top();
    }
};

int main()
{
    vector<int> v = { 2,7,4,1,8,1 };
    cout << Solution().lastStoneWeight(v) << endl;

    return 0;
}